package com.ruoyi.common.base.biz.controller;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.base.biz.service.biz.BaseBizService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.BizBaseEntity;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.valid.VAdd;
import com.ruoyi.common.valid.VEdit;
import com.ruoyi.common.valid.VList;
import com.ruoyi.common.valid.VPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * BaseController
 *
 * @author HHKJ-SERVER-ZJ
 * @since 2023/6/13 16:22
 */
public class BaseController<E extends BizBaseEntity, Req extends E, Res extends E,
        BizS extends BaseBizService<E, Req, Res>> extends com.ruoyi.common.core.controller.BaseController
{

    @Autowired
    private BizS bizService;

    public String getPermiPrefix()
    {
        return "";
    }

    public BizS getBizService()
    {
        return bizService;
    }

    /** 详细信息 */
    @PreAuthorize("@ss.hasPermi(this, 'query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getById(@Validated @NotNull(message = "主键不能为空") @PathVariable("id") Long id)
    {
        return success(getBizService().getById(id));
    }

    /** 列表 */
    @PostMapping("/list")
    public TableDataInfo list(@Validated(VList.class) @RequestBody Req req)
    {
        List<Res> list = this.getBizService().list(req);
        return getDataTable(list);
    }

    /** 分页 */
    @PreAuthorize("@ss.hasPermi(this, 'page')")
    @PostMapping("/page")
    public TableDataInfo page(@Validated(VPage.class) @RequestBody Req req)
    {
        startPageBody(req);
        PageInfo<Res> pageRes = this.getBizService().paging(req);
        return getDataTable(pageRes);
    }

    /** 修改 */
    @PreAuthorize("@ss.hasPermi(this, 'edit')")
    @Log(title = "修改", businessType = BusinessType.UPDATE)
    @PutMapping()
    public AjaxResult update(@Validated(VEdit.class) @RequestBody Req req)
    {
        return toAjax(this.getBizService().update(req));
    }

    /** 新增 */
    @PreAuthorize("@ss.hasPermi(this, 'add')")
    @Log(title = "新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult save(@Validated(VAdd.class) @RequestBody Req req)
    {
        return toAjax(this.getBizService().save(req));
    }

    /** 删除 */
    @PreAuthorize("@ss.hasPermi(this, 'remove')")
    @Log(title = "删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{idList}")
    public AjaxResult removeByIdList(@PathVariable List<Long> idList)
    {
        return toAjax(this.getBizService().removeByIdList(idList));
    }
}
